package com.weipeep.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.weipeep.common.utils.poi.ProductData;
import com.weipeep.sys.vo.ProductResponse;
import com.weipeep.sys.vo.ProductSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weipeep.sys.dao.ProductDao;
import com.weipeep.sys.domain.ProductDO;
import com.weipeep.sys.service.ProductService;
import com.weipeep.common.base.CoreServiceImpl;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-20 22:55:14 | wangyun</small>
 */
@Service
public class ProductServiceImpl extends CoreServiceImpl<ProductDao, ProductDO> implements ProductService {
    @Resource
    private  ProductDao productDao;
    @Override
    public List<ProductResponse> fetchLatestProducts(Long userId,Integer pageNumber, Integer pageSize,Integer hotsell) {
        Page page = new Page(pageNumber,pageSize);
        Integer offset = page.getOffset();
        Integer limit = page.getLimit();
        Map<String,Object> map = Maps.newHashMap();
        map.put("offset",offset);
        map.put("limit",limit);
        if (userId!=null){
            map.put("userId",userId);
        }
        if (hotsell!=null){
            map.put("hotsell",hotsell);
        }
        List<ProductResponse> productResponseList = Lists.newArrayList();
        List<Long> userIds = productDao.fetchLatestProductUsers(map);
        if (userIds!=null && userIds.size()>0){
            for (Long tempUSerId:userIds){
                Map<String,Object> temp = Maps.newHashMap();
                temp.put("userId",tempUSerId);
                if (hotsell!=null){
                    temp.put("hotsell",hotsell);
                }
                List<ProductResponse> productResponses = productDao.fetchOnePersonLatestProducts(temp);
                productResponseList.addAll(productResponses);
            }
        }
        return productResponseList;
    }

    @Override
    public Page<ProductResponse> selectProductPage(Page<ProductResponse> page, ProductSearch productSearch) {
        List<ProductResponse> productDOList = productDao.selectProductListByPage(productSearch);
        page.setRecords(productDOList);
        return page;
    }

    @Override
    public int selectProductCount(ProductSearch productSearch) {
        int count = productDao.selectProductCountByPage(productSearch);
        return count;
    }

    @Override
    public void refresh(Map<String, Object> map) {
        productDao.refresh(map);
    }

    @Override
    public List<ProductResponse> fetchOwnLatestProducts(Long userId, Integer pageNumber, Integer pageSize, Integer hotsell) {
        Page page = new Page(pageNumber,pageSize);
        Integer offset = page.getOffset();
        Integer limit = page.getLimit();
        Map<String,Object> map = Maps.newHashMap();
        map.put("offset",offset);
        map.put("limit",limit);
        if (userId!=null){
            map.put("userId",userId);
        }
        if (hotsell!=null){
            map.put("hotsell",hotsell);
        }
        List<ProductResponse> productResponseList = productDao.fetchOwnLatestProducts(map);
        return productResponseList;
    }

}
